# coding:utf-8
"""
请求禅道相关方法
"""
import requests
from config import ZenTaoConfig
from password import ZenTaoPass
from .utils import recursive_json_loads
from app.extensions import log


class ZenTao:
    _sessionName = ""
    _sessionID = ""

    def __init__(self):
        if ZenTaoConfig.IS_USE_ZENTAO:
            self._doZenTaoLogin()
        pass

    """
    登陆禅道系统
    """

    def _doZenTaoLogin(self):
        _session = self._doPost(ZenTaoConfig.ZENTAO_GETSESSION_URL)
        self._sessionID = _session.data.sessionID
        self._sessionName = _session.data.sessionName
        self._doPost('%s&account=%s&password=%s' % (
            ZenTaoConfig.ZENTAO_USER_LOGIN_URL, ZenTaoPass.ZENTAO_USERNAME, ZenTaoPass.ZENTAO_PASSWORD))

    def _getUrl(self, url):
        _url = "%s%s" % (ZenTaoPass.ZENTAO_URL, url)
        if ZenTaoConfig.ZENTAO_GETSESSION_URL == url:
            pass
        else:
            if "?" in url:
                _url = "%s&%s=%s" % (_url, self._sessionName, self._sessionID)
            else:
                _url = "%s?%s=%s" % (_url, self._sessionName, self._sessionID)
        return _url

    def _doPost(self, url):
        _url = self._getUrl(url)
        if 'f=login' not in _url:
            log.info("_url=%s" % _url)
        _obj = recursive_json_loads(requests.post(url = _url).content)
        # 判断是否为登陆状态
        try:
            if _obj.data.keepLogin == 'off' and _obj.data.title == 'Login':
                if not ZenTaoConfig.IS_USE_ZENTAO:
                    return None
                else:
                    log.info('app.modules.common.zentao.ZenTao#_doPost. zentao login timeout. do login again.')
                    self._doZenTaoLogin()
                    _url = self._getUrl(url)
                    _obj = recursive_json_loads(requests.post(url = _url).content)
        except:
            pass
        return _obj

    def doPost(self, url):
        if "" == self._sessionID:
            log.error("zentao sessionID is null . url=%s" % url)
            return None
        else:
            _obj = self._doPost(url)
        return _obj

    """
    公司信息
    """

    def CompanyInfo(self):
        _obj = self.doPost(ZenTaoConfig.ZENTAO_COMPANY_VIEW_URL)
        if _obj is not None and _obj.status == 'success':
            log.info("app.modules.common.zentao.ZenTao#CompanyInfo %s " % _obj.data.company)
            return _obj.data.company
        else:
            log.error("app.modules.common.zentao.ZenTao#CompanyInfo . obj is %s " % _obj)
            return None

    """
    禅道用户列表
    """

    def UserList(self):
        _obj = self.doPost(ZenTaoConfig.ZENTAO_COMPANY_BROWSE_URL)
        if _obj is not None and _obj.status == 'success':
            log.info("app.modules.common.zentao.ZenTao#UserList len=%s " % len(_obj.data.users))
            return _obj.data.users
        else:
            log.error("app.modules.common.zentao.ZenTao#UserList . obj is %s " % _obj)
            return None

    """
    项目列表
    """

    def ProjectList(self):
        _obj = self.doPost(ZenTaoConfig.ZENTAO_PROJECT_LIST_URL)
        if _obj is not None and _obj.status == 'success':
            log.info("app.modules.common.zentao.ZenTao#ProjectList len=%s " % len(_obj.data.projects))
            return _obj.data.projects
        else:
            log.error("app.modules.common.zentao.ZenTao#ProjectList . obj is %s " % _obj)
            return None

    """
    项目成员
    """

    def ProjectTeam(self, projectId):
        _url = '%s&projectID=%s' % (ZenTaoConfig.ZENTAO_PROJECT_TEAM_URL, projectId)
        _obj = self.doPost(_url)
        if _obj is not None and _obj.status == 'success':
            log.info("app.modules.common.zentao.ZenTao#ProjectTeam len=%s " % len(_obj.data.teamMembers))
            return _obj.data.teamMembers
        else:
            log.error("app.modules.common.zentao.ZenTao#ProjectTeam . obj is %s " % _obj)
            return None

    """
    项目信息
    """

    def ProjectView(self, projectId):
        _url = '%s&projectID=%s' % (ZenTaoConfig.ZENTAO_PROJECT_VIEW_URL, projectId)
        _obj = self.doPost(_url)
        if _obj is not None and _obj.status == 'success':
            log.info("app.modules.common.zentao.ZenTao#ProjectView len=%s " % _obj.data)
            return _obj.data.project
        else:
            log.error("app.modules.common.zentao.ZenTao#ProjectView . obj is %s " % _obj)
            return None

    def ProjectGroup(self):
        _obj = self.doPost(ZenTaoConfig.ZENTAO_COMPANY_GROUP_URL)
        if _obj is not None and _obj.status == 'success':
            log.info("app.modules.common.zentao.ZenTao#ProjectGroup len=%s " % len(_obj.data.groupUsers))
            return _obj.data.groupUsers
        else:
            log.error("app.modules.common.zentao.ZenTao#ProjectGroup . obj is %s " % _obj)
            return None

